﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;

namespace Parallel3M.parallel3m.framework
{
    public class ParallelPayload
    {
        public const string ID = "id";
        public const string ROUTETAG = "routeTag";
        public const string DOMAIN = "domain";
        public const string TYPE = "type";
        public const string HANDLEID = "handlerid";
        public const string ACCESS = "access";
        public const string LANGUAGE = "language";
        public const string TIMESTAMP = "timestamp";
        public const string REQUEST = "request";
        public const string RESPONSE = "response";
        public const string ROUTECOUNT = "routeCount";
        public const string PCSSOURCE = "pcss";
        public const string PCSDESTINATION = "pcsd";
        
        public string id;
        public string pcs_source;  //source pcs id
        public string pcs_des;     //destination pcs id
        public string routeTag=PAYLOADCONSTRANT.DEFAULT_ROUTETAG;    //control payload flow, for example the cycle task payload flow 
        public int routeCount = -1;  //-1 means no route count; 0 means this is end at the last processor
        public string domain;     //payload domain, subscribe for the special payload
        public string type;       //request | response | command
        public string handlerid;  //how to handle payload: the class name as usual
        public string access;     //public | private 
        public string language;   //different compute language: java, c#, perl, python,
        public long timestamp;    //generated timestamp  
        public string request;    //the xml format or json format
        public string response;   //payload handler result
        public String status;
        public bool isCompleted = false;

        public virtual string serialize()
        {
            return response;
        }


        /*
        <payload id="" timestamp="" pcss="*" pcsd="*" routeTag="*" domain="*" type="request" handlerid="CSHARP_RUN" access="Public" language="c#">
            <request><function>Parallel3M.parallel3m.function.GetTextLength</function><text>computer</text></request>
         *  <response/>
        </payload>
        */
        public virtual void parse(String payload_defineXml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(payload_defineXml);
            XmlNodeList nodes = doc.ChildNodes;
            XmlNode root = nodes[0];
            parse(root);

        }

        public virtual void parse(XmlNode node)
        {
            XmlAttributeCollection atts = node.Attributes;
            Hashtable attTable = new Hashtable();
            foreach (XmlAttribute att in atts)
            {
                attTable.Add(att.Name, att.Value);
            }
            id = (String)attTable["id"];
            if (id == null || id == "")
            {
                id = Guid.NewGuid().ToString();
            }
            try
            {
                timestamp = long.Parse((string)attTable["timestamp"]);
            }
            catch 
            {
                timestamp = -1;
            }
            routeTag=(String)attTable["routeTag"];
            if(routeTag==null){
                routeTag = PAYLOADCONSTRANT.DEFAULT_ROUTETAG;
            }

            domain=(String)attTable["domain"];
            type=(String)attTable["type"];
            access=(String)attTable["access"];
            handlerid=(String)attTable["handlerid"];
            language=(String)attTable["language"];
            pcs_source = (String)attTable["pcss"];
            pcs_des = (String)attTable["pcsd"];

            string routeCount_str = (String)attTable["routeCount"];
            if (routeCount_str == null)
            {
                routeCount = -1;
            }
            else
            {
                routeCount = Int16.Parse(routeCount_str);
            }


            foreach(XmlNode childnode in node.ChildNodes)
            {
                if(childnode.Name == "request"){
                    request = childnode.OuterXml.ToString();
                }
                else if(childnode.Name == "response"){
                    response=childnode.InnerText;
                }
            }
            

        }

    }
}
